home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Online / hsc / source / ugly / args_hlp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-02  |  4.7 KB  |  208 lines

  1. /*
  2.  * This source code is part of hsc, a html-preprocessor,
  3.  * Copyright (C) 1993-1997  Thomas Aglassinger
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  */
  20. /*
  21.  * ugly/args_hlp.c
  22.  *
  23.  * sub-module for ugly/args.c
  24.  *
  25.  * ugly argument handling help functions
  26.  *
  27.  * Copyright (C) 1994,95  Thomas Aglassinger
  28.  *
  29.  * This program is free software; you can redistribute it and/or modify
  30.  * it under the terms of the GNU General Public License as published by
  31.  * the Free Software Foundation; either version 2 of the License, or
  32.  * (at your option) any later version.
  33.  *
  34.  * This program is distributed in the hope that it will be useful,
  35.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  37.  * GNU General Public License for more details.
  38.  *
  39.  * You should have received a copy of the GNU General Public License
  40.  * along with this program; if not, write to the Free Software
  41.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  42.  *
  43.  * updated: 10-Sep-1996
  44.  * created:  3-Jul-1994
  45.  *
  46.  */
  47.  
  48. /*
  49.  * includes
  50.  */
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <stdarg.h>
  54. #include <string.h>
  55. #include <ctype.h>
  56.  
  57. #include "utypes.h"
  58. #include "umemory.h"
  59. #include "ustring.h"
  60. #include "dllist.h"
  61.  
  62. #define NOEXTERN_UGLY_ARGS_H
  63. #include "uargs.h"
  64.  
  65. /*
  66.  * strcat_flag
  67.  */
  68. static VOID strcat_flag(STRPTR s, struct arginfo *ai, ULONG chk_flag, char ch)
  69. {
  70.     char flag[3] = "/x";
  71.  
  72.     if ((ai->ai_flags) & chk_flag)
  73.     {
  74.         flag[1] = ch;
  75.         strcat(s, flag);
  76.     }
  77. }
  78.  
  79. static STRPTR ai2str(struct arginfo *ai)
  80. {
  81.     static STRARR s[100];
  82.  
  83.     strncpy(s, ai->ai_id, 100 - 20);
  84.  
  85.     switch (ai->ai_type)
  86.     {
  87.     case ARG_SWITCH:
  88.         strcat(s, "/S");
  89.         break;
  90.     case ARG_LONG_RANGE:
  91.     case ARG_LONG:
  92.         strcat(s, "/N");
  93.         break;
  94.     case ARG_TEXT:
  95.     case ARG_ENUM:
  96.     case ARG_HANDLEFUNC:
  97.         break;
  98.     default:
  99.         strcat(s, "/?");
  100.         break;
  101.     }
  102.  
  103.     strcat_flag(s, ai, ARG_KEYWORD, 'K');
  104.     strcat_flag(s, ai, ARG_REQUIRED, 'A');
  105.     strcat_flag(s, ai, ARG_MULTIPLE, 'M');
  106.     strcat_flag(s, ai, ARG_CASESENS, 'C');
  107.  
  108.     return (s);
  109. }
  110.  
  111. /*
  112.  * fprintf_arghelp
  113.  */
  114. int fprintf_arghelp(FILE * stream, struct arglist *al)
  115. {
  116.     int err = 0;
  117.  
  118.     if (al)
  119.     {
  120.         struct dlnode *nd;
  121.         struct arginfo *ai;
  122.         size_t maxidlen = 0;      /* max. length if arg id */
  123.  
  124.         /* compute maximum length */
  125.         nd = al->al_list->first;
  126.         while (nd)
  127.         {
  128.             ai = (struct arginfo *) dln_data(nd);
  129.  
  130.             if (ai)
  131.             {
  132.                 if (ai->ai_help)
  133.                 {
  134.                     STRPTR s = ai2str(ai);
  135.                     if (strlen(s)>maxidlen)
  136.                         maxidlen = strlen(s);
  137.                 }
  138.             }
  139.             nd = dln_next(nd);
  140.         }                       /*while */
  141.         maxidlen += 2;
  142.  
  143.         nd = al->al_list->first;
  144.         while (nd)
  145.         {
  146.             ai = (struct arginfo *) dln_data(nd);
  147.  
  148.             if (ai)
  149.             {
  150.                 if (ai->ai_help)
  151.                 {
  152.                     STRPTR s = ai2str(ai);
  153.  
  154.                     if (ai->ai_help)
  155.                         fprintf(stream, " %-*s  %s",
  156.                                 (int) maxidlen, s, ai->ai_help);
  157.                     else
  158.                         fprintf(stream, "%s", s);
  159.  
  160.                     fprintf(stream, "\n");
  161.                 }
  162.             }
  163.  
  164.             nd = dln_next(nd);
  165.         }                       /*while */
  166.     }
  167.  
  168.     return err;
  169. }
  170.  
  171. /*
  172.  * fprintf_arghelp_short
  173.  *
  174.  * print short template line help
  175.  */
  176. int fprintf_arghelp_short(FILE * stream, struct arglist *al)
  177. {
  178.     int err = 0;
  179.  
  180.     if (al)
  181.     {
  182.         struct dlnode *no;
  183.         struct arginfo *ai;
  184.  
  185.         no = al->al_list->first;
  186.  
  187.         if (no)
  188.             fprintf(stream, "Usage: ");
  189.  
  190.         while (no)
  191.         {
  192.             ai = (struct arginfo *) no->data;
  193.  
  194.             if (ai)
  195.                 fprintf(stream, "%s", ai2str(ai));
  196.  
  197.             no = no->next;
  198.             if (no)
  199.                 fprintf(stream, ",");
  200.         }                       /*while */
  201.  
  202.         fprintf(stream, "\n");
  203.     }
  204.  
  205.     return err;
  206. }
  207.  
  208.